package com.yingpai.fitness.dvp_2_0.main.fragment.shop;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.main.activity.shop.ShopDetailActivity;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.fragment.CommonFragment;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.adapter.shop.ShopSaleRecyclerAdapter;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.ShopSaleData;
import com.yingpai.fitness.dvp_2_0.main.widget.CustomGridLayoutManage;
import com.yingpai.fitness.dvp_2_0.main.widget.ParentRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class ShopSaleFragment extends CommonFragment implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    private int categoryId;
    private int pageNum = 1;
    private int pageSize = 10;
    private int type;
    private int brandId;
    private String content;
    private ShopSaleRecyclerAdapter mAdapter;
    Unbinder unbinder;
    @BindView(R.id.shop_sale_fragment_rv_list)
    ParentRecyclerView shop_sale_fragment_rv_list;
    @BindView(R.id.swiperefresh)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.title_bar)
    RelativeLayout title_bar;
    @BindView(R.id.empty)
    LinearLayout empty;

    /**
     * 推荐写法
     *
     * @param
     * @return
     */
    public static ShopSaleFragment getInstance(int categoryId, int type, int brandId, String content) {
        ShopSaleFragment mShopSaleFragment = new ShopSaleFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("categoryId", categoryId);
        bundle.putInt("type", type);
        bundle.putInt("brandId", brandId);
        bundle.putString("content", content);

        mShopSaleFragment.setArguments(bundle);
        return mShopSaleFragment;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.shop_sale_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        title_bar.setVisibility(View.GONE);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(R.color.common_color);

        categoryId = getArguments().getInt("categoryId", -1);
        type = getArguments().getInt("type", -1);
        brandId = getArguments().getInt("brandId", -1);
        content = getArguments().getString("content");

        /**
         * 设置布局管理器
         */
        shop_sale_fragment_rv_list.setLayoutManager(new CustomGridLayoutManage(context, 2));
        mAdapter = new ShopSaleRecyclerAdapter(R.layout.shop_sale_list_item);
        mAdapter.setOnLoadMoreListener(this, shop_sale_fragment_rv_list);
        mAdapter.setOnItemChildClickListener(this);
        shop_sale_fragment_rv_list.setAdapter(mAdapter);

        return view;
    }

    @Override
    protected void initData() {
        showDialog(context, null);
        HttpParams httpParams = new HttpParams();
        httpParams.put("categoryId", categoryId);
        httpParams.put("pageNum", pageNum++);
        httpParams.put("pageSize", pageSize);
        httpParams.put("type", type);
        httpParams.put("brandId", brandId);
        httpParams.put("cont", content);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.SHOP_LIST, httpParams, Url.SHOP_LIST, new Callback<ShopSaleData>() {
            @Override
            public void onSuccess(ShopSaleData shopSaleData) {
                if (Constant.REQUEST_SUCCESS.equals(shopSaleData.getResult())) {
                    if (pageNum - 1 == 1) {
                        if(shopSaleData.getMap().getPageInfo().getTotal()>0){
                            empty.setVisibility(View.GONE);
                            mAdapter.setNewData(shopSaleData.getMap().getPageInfo().getList());
                        }else{
                            empty.setVisibility(View.VISIBLE);
                        }
                    } else {
                        mAdapter.addData(shopSaleData.getMap().getPageInfo().getList());
                    }
                    swipeRefreshLayout.setRefreshing(false);
                    mAdapter.notifyDataSetChanged();

                    int pagecount;
                    if (shopSaleData.getMap().getPageInfo().getTotal() % pageSize == 0) {
                        pagecount = shopSaleData.getMap().getPageInfo().getTotal() / pageSize;
                    } else {
                        pagecount = shopSaleData.getMap().getPageInfo().getTotal() / pageSize + 1;
                    }

                    if (pageNum > pagecount) {
                        mAdapter.loadMoreEnd();
                    } else {
                        mAdapter.loadMoreComplete();
                        mAdapter.setEnableLoadMore(true);
                    }
                } else {
                    if (pageNum - 1 == 1) {
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        empty.setVisibility(View.GONE);
                    }
                    showToast(shopSaleData.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
                if (pageNum - 1 == 1) {
                    empty.setVisibility(View.VISIBLE);
                } else {
                    empty.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    @OnClick({R.id.empty})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.empty:
                onRefresh();
                break;
        }
    }


    /**
     * 适配器监听
     *
     * @param adapter
     * @param view
     * @param position
     */
    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.listitem_zone:
                Bundle bundle = new Bundle();
                bundle.putInt("id", mAdapter.getItem(position).getId());
                startActivity(ShopDetailActivity.class, bundle);
                break;
        }
    }

    @Override
    public void onRefresh() {
        pageNum = 1;
        initData();
    }

    @Override
    public void onLoadMoreRequested() {
        initData();
    }

}
